# Reference: <https://u-boot.readthedocs.io/en/latest/board/amlogic/radxa-zero.html>
# Maintainer: exkc <exxxxkc@getgoogleoff.me>
pkgname=u-boot-radxa-zero
pkgver=2023.01
pkgrel=1
pkgdesc="U-Boot bootloader for the PINE64 PinePhone"
url="https://source.denx.de/u-boot"
arch="aarch64"
license="GPL-2.0-or-later OFL-1.1 BSD-2-Clause BSD-3-Clause eCos-2.0 IBM-pibs
	ISC LGPL-2.0-only LGPL-2.1-only X11 proprietary"
makedepends="$depends_dev
	bc
	bison
	dtc
	flex
	openssl-dev
	py3-setuptools
	python3-dev
	swig
	bash
	"
options="!check"
# https://u-boot.readthedocs.io/en/latest/board/amlogic/pre-generated-fip.html
_amlogicblob_commit="5b8beba2f50ce06219c4e4844101226a81aef854"
source="https://source.denx.de/u-boot/u-boot/-/archive/v$pkgver/u-boot-v$pkgver.tar.gz
	amlogicblob.tar.gz::https://github.com/radxa/fip/archive/$_amlogicblob_commit.tar.gz
	"
builddir="$srcdir/u-boot-v$pkgver"

build() {
	touch include/config.h
	LC_ALL=C date +'#define U_BOOT_DATE "%b %d %C%y"' > include/timestamp_autogenerated.h
	LC_ALL=C date +'#define U_BOOT_TIME "%T"' >> include/timestamp_autogenerated.h

	export BUILD_DIR="$builddir"/build
	mkdir -p "$BUILD_DIR"
	make O="$BUILD_DIR" HOSTCC=gcc ARCH=arm radxa-zero_defconfig
	make O="$BUILD_DIR" HOSTCC=gcc ARCH=arm all
	cd $srcdir/fip-$_amlogicblob_commit/radxa-zero
	cp $srcdir/u-boot-v$pkgver/build/u-boot.bin ./bl33.bin
	make
	dd if=u-boot.bin.sd.bin of=u-boot.bin.sd-stripped.bin conv=fsync,notrunc bs=512 skip=1

}

package() {
	install -D -m644 "$srcdir/fip-$_amlogicblob_commit/radxa-zero/u-boot.bin.sd-stripped.bin" \
		"$pkgdir/usr/share/u-boot/radxa-zero/u-boot.bin.sd-stripped.bin"
	install -D -m644 "$srcdir/fip-$_amlogicblob_commit/radxa-zero/u-boot.bin.sd.bin" \
		"$pkgdir/usr/share/u-boot/radxa-zero/u-boot.bin.sd.bin"
	install -D -m644 "$srcdir/fip-$_amlogicblob_commit/radxa-zero/u-boot.bin" \
		"$pkgdir/usr/share/u-boot/radxa-zero/u-boot.bin"
	install -D -m644 "$srcdir/fip-$_amlogicblob_commit/radxa-zero/u-boot.bin.usb.bl2" \
		"$pkgdir/usr/share/u-boot/radxa-zero/u-boot.bin.usb.bl2"
	install -D -m644 "$srcdir/fip-$_amlogicblob_commit/radxa-zero/u-boot.bin.usb.tpl" \
		"$pkgdir/usr/share/u-boot/radxa-zero/u-boot.bin.usb.tpl"
}

sha512sums="
c25aae1b0d2f677d295587c6d318f23d52bc3a79e968bed9093645943a9840c56b1f6f44e5649d236e76e3dc85f0473c1da4a5205c02dcf437bca820acaf6fb8  u-boot-v2023.01.tar.gz
3ac5fa8f0ef702585de71a8e0364088639b1163e84d5159b7768dda0016eeb97229bd089acfd0559b0ac32ea0d4ee15945c53e5684c0030e089414ff4135aed9  amlogicblob.tar.gz
"
